package com.ld.admin.tag.plugin;

import java.io.IOException;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.ld.shieldsb.common.core.util.StringUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 
 * 上传图片标签
 * 
 * @ClassName ImagesUploadPluginTag
 * @author <a href="mailto:550827627@qq.com" target="_blank">刘金浩</a>
 * @date 2019年7月18日 上午11:38:42
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ImagesUploadPluginTag extends TagSupport {

    private String imgSource;
    private String name;
    private boolean multi = false;
    private String buttonText = "上传图片";
    private String shieldSettings;
    private boolean required = false;
    private boolean show = false;

    private String imgageWidth = "70";
    private String imgageHeight = "70";
    /**
     * @Fields serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    public int doStartTag() throws JspTagException {
        return EVAL_PAGE;
    }

    public int doEndTag() throws JspTagException {
        JspWriter out = null;
        try {
            out = this.pageContext.getOut();
            out.print(end().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }

    public StringBuffer end() {
        StringBuffer sb = new StringBuffer();

        sb.append("<div class=\"clearfix uploadContainer\">");
        sb.append(" <!-- imgPath存放图片路径 -->\r\n");
        sb.append("<input id=\"image\" name=\"" + name + "\" class=\"imgPath " + (required ? "required" : "ignore")
                + "\" type=\"hidden\" value=\"" + imgSource + "\"/>");
        sb.append("<div class=\"img\" style=\"margin-right: 10px; float: left; \">");
        // 设置图片路径
        showImages(sb, imgSource);

        sb.append("</div>");
        if (!show) {
            sb.append("<div class=\"option\" style=\"float: left;\" >");
            sb.append("<!-- imageUpload上传图片 -->\r\n");
            sb.append("<div class=\"ms imageUpload plugin_imgUpload\" style=\"padding-top:10px;\" " + (multi ? "data-append='true'" : "")
                    + "");
            setShieldSettings(sb);
            setUplodFileNumber(sb);
            sb.append("  ><input type=\"button\" class=\"submit button\" value=\"" + buttonText + "\"></div>");
            sb.append(" <div class=\"tips text-danger\">支持的图片类型：JPG,JPEG,GIF,BMP,PNG,限制大小2M</div>");
            sb.append("</div>");
            sb.append("</div>");
        }

        return sb;
    }

    public void showImages(StringBuffer sb, String imgSource) {

        if (StringUtils.isNotEmpty(imgSource)) {
            sb.append("<!-- imgShow预览图片 -->\r\n");
            String[] image = imgSource.split("#");
            for (int i = 0; i < image.length; i++) {
                sb.append(" <div style=\"float: left;\">");
                sb.append("<img id=\"imageNews\" class=\"imgShow  imgPreview_plugin \" name=\"imageNews\" width=\"" + imgageWidth
                        + "\" height=\"" + imgageHeight + "\" src=\"" + image[i]
                        + "\" style=\"margin: 0 5px 10px; border: 1px solid #eee;  \">");
                if (!show) {
                    sb.append("<div style=\"text-align: center;\" class=\"delimageNews\" ><a>删除</a></div>");
                }
                sb.append("</div>");
            }

        } else {

            sb.append("<div style=\"float: left;\">");
            sb.append("<img id=\"imageNews\" class=\"imgShow nopic\" name=\"imageNews\" width=\"70\" height=\"70\" src=\""
                    + pageContext.getServletContext().getAttribute("ctxPath") + "/images/project/nopic.jpg\">");
            sb.append(" </div>");
        }

    }

    public StringBuffer setShieldSettings(StringBuffer sb) {
        if (StringUtils.isNotEmpty(shieldSettings)) {
            sb.append("shieldSettings=\"" + shieldSettings + "\"");
        }
        return sb;
    }

    public StringBuffer setUplodFileNumber(StringBuffer sb) {
        if (StringUtils.isNotEmpty(imgSource)) {
            String[] image = imgSource.split("#");
            int imageNum = image.length;
            sb.append(" data-filenumber=\"" + imageNum + "\"");
        }
        return sb;
    }

    public String getShieldSettings() {
        return shieldSettings;
    }

    public void setShieldSettings(String shieldSettings) {
        this.shieldSettings = shieldSettings;
    }

}
